package br.com.controller.professor;

import java.io.IOException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.arquitetura.controlador.AbstractMBean;
import br.com.arquitetura.excecoes.BDException;
import br.com.controller.acesso.MBAcessoUsuario;
import br.com.data.hibernate.ProfessorHibernateDAO;
import br.com.model.entidade.Professor;
import br.com.model.entidade.Usuario;

@ManagedBean
@ViewScoped
public class MBIndexProfessor extends AbstractMBean<Professor>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ProfessorHibernateDAO professorHibernateDAO;
	private Professor Professor;
	private Long idIrAula;
	
	public MBIndexProfessor() {
		
		//Limpando as variaveis de session desnecessárias
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.removeAttribute("idAulaVizualacao");
		
		
		
		//Iniciando variaveis necessárias para a pagina
		this.professorHibernateDAO = (ProfessorHibernateDAO) getBean("professorHibernateDAO");
		MBAcessoUsuario mb = new MBAcessoUsuario();
		Usuario user = mb.getUsuario();
		List<Professor> lista;
		try {
			lista = professorHibernateDAO.getByField(Professor.class, "usuario", user);
			if (!lista.isEmpty()){
				Professor = lista.get(0);
			}
		} catch (BDException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

	public void irTurma(){
		
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
		session.setAttribute("idAulaVizualizacao", idIrAula);
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("turma/index.jsf");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("pagina nao encontrada");
		}
		
	}
	
	public Professor getProfessor() {
		return Professor;
	}

	public void setProfessor(Professor Professor) {
		this.Professor = Professor;
	}

	public Long getIdIrAula() {
		return idIrAula;
	}

	public void setIdIrAula(Long idIrAula) {
		this.idIrAula = idIrAula;
	}
	
	
}
